package com.bm.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bm.common.dto.StudentDto;
import com.bm.common.dto.TeacherDto;
import com.bm.common.util.BeanUtil;
import com.bm.common.util.Rutils;
import com.bm.common.vo.R;
import com.bm.server.dao.StudentDao;
import com.bm.server.dao.TeacherDao;
import com.bm.server.entity.Student;
import com.bm.server.entity.Teacher;
import com.bm.server.service.intf.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: 赵金帅
 * @Date: 2022/07/24/14:48
 */
@Service
public class TeacherServiceimpl implements TeacherService {
    @Resource
    private StudentDao stuDao;
    @Resource
    private TeacherDao teaDao;
    @Override
    public R selectTea() {
        return Rutils.ok(teaDao.selectList(null));
    }

    @Override
    public R selectStu(Integer tid) {
        if (tid!=null){
            return Rutils.ok(stuDao.selectList(new QueryWrapper<Student>().eq("tid",tid)));
        }
        return Rutils.fail();
    }

    @Override
    public R saveStu(StudentDto dto) {
        if (dto!=null){
            Student student = BeanUtil.copy(dto, dto.getClass().getDeclaredFields(), Student.class);
            if (stuDao.insert(student)>0){
                return Rutils.ok();
            }
        }
        return Rutils.fail();
    }

    @Override
    public R saveTea(TeacherDto dto) {
        if (dto!=null){
            Teacher teacher = BeanUtil.copy(dto, dto.getClass().getDeclaredFields(), Teacher.class);
            if (teaDao.insert(teacher)>0){
                return Rutils.ok();
            }
        }
        return Rutils.fail();
    }
}
